﻿package org.dm.3d.Actions.object 
{
	import org.dm.3d.Actions.Action;
	import org.dm.3d.geom.Num3D;
	import org.dm.3d.geom.MathExtend;
	/**
	 * ...
	 * @author Scott
	 */
	public class Eiffel extends Action
	{
		private var goalPos:Array;
		private var goalNum:uint;
		private var _init:Boolean = false;
		private var _pre:Boolean = true;
		private var _root:Boolean = false;
		private var _cur:uint = 0;
		private var _speed:uint = 30;
		
		public function Eiffel() 
		{
			this.isUsed = true;
			goalPos = new Array();
			var points:Vector.<Number> = Vector.<Number>([59.5,140,-65.2,35.9,140,-65.2,-12.5,140,-65.2,-61.1,140,-65.2,-84.6,140,-65.2,53.3,104,-65.2,20.4,104,-65.2,-12.5,104,-65.2,-45.5,104,-65.2,-78.5,104,-65.2,53.3,104,-26.6,37.1,104,-46.1,-12.5,104,-46.1,-62.2,104,-46.1,-78.5,104,-26.6,53.3,104,11.9,37.1,104,11.9,-62.2,104,11.9,-78.5,104,11.9,53.3,104,50.4,37.1,104,70,-12.5,104,70,-62.2,104,70,-78.5,104,50.4,53.3,104,89,20.4,104,89,-12.5,104,89,-45.5,104,89,-78.5,104,89,59.5,140,89,29.9,140,89,-12.5,140,89,-55.1,140,89,-84.6,140,89,59.5,140,68.8,-12.5,140,49.7,-84.6,140,64.1,59.5,140,11.9,23.4,140,11.9,-12.5,140,11.9,-48.6,140,11.9,-84.6,140,11.9,59.5,140,-44.9,-12.5,140,-25.9,-84.6,140,-40.3,20.4,19,-26.6,-12.5,19,-18.2,20.4,19,11.9,-45.5,19,-26.6,-45.5,19,11.9,-12.5,19,42.1,20.4,19,50.4,-45.5,19,50.4,32.1,81,-40.3,32.1,81,64.1,-57.2,81,64.1,-57.2,81,-40.3,20.4,7,-26.6,20.4,7,11.9,-45.5,7,-26.6,-45.5,7,11.9,20.4,7,50.4,-45.5,7,50.4,28.5,19,-36.4,-12.5,19,-38.7,-12.5,8,-38.7,28.5,7,-36.4,30.3,19,11.9,30.3,8,11.9,-53.7,19,-36.4,-53.7,7,-36.4,-55.5,19,11.9,-55.5,8,11.9,28.5,19,60.2,-12.5,19,62.5,28.5,7,60.2,-12.5,8,62.5,-53.7,19,60.2,-53.7,7,60.2,20.4,7,-26.6,-12.5,7,-27,20.4,7,11.9,-45.5,7,-26.6,-45.5,7,11.9,-12.5,7,50.8,20.4,7,50.4,-45.5,7,50.4,-2.4,-243,-1.5,-12.5,-243,-1.5,-2.4,-243,11.9,-22.7,-243,-1.5,-22.7,-243,11.9,-12.5,-243,25.3,-2.4,-243,25.3,-22.7,-243,25.3,-39.6,-8,-19.4,-12.5,-8,-19.4,14.4,-8,-19.4,14.4,-8,11.9,14.4,-8,43.3,-12.5,-8,43.3,-39.6,-8,43.3,-39.6,-8,11.9,-35.3,-23,-18.1,-12.5,-23,-18.1,10.1,-23,-18.1,10.1,-23,11.9,10.1,-23,41.9,-12.5,-23,41.9,-35.3,-23,41.9,-35.3,-23,11.9,-33,-39,-15.1,-12.5,-39,-15.1,7.8,-39,-15.1,7.8,-39,11.9,7.8,-39,38.9,-12.5,-39,38.9,-33,-39,38.9,-33,-39,11.9,-30.6,-55,-11.9,-12.5,-55,-11.9,5.4,-55,-11.9,5.4,-55,11.9,5.4,-55,35.7,-12.5,-55,35.7,-30.6,-55,35.7,-30.6,-55,11.9,-29.2,-73,-10,-12.5,-73,-10,4,-73,-10,4,-73,11.9,4,-73,33.9,-12.5,-73,33.9,-29.2,-73,33.9,-29.2,-73,11.9,-27,-92,-7.2,-12.5,-92,-7.2,1.8,-92,-7.2,1.8,-92,11.9,1.8,-92,31,-12.5,-92,31,-27,-92,31,-27,-92,11.9,-25.8,-108,-5.5,-12.5,-108,-5.5,0.6,-108,-5.5,0.6,-108,11.9,0.6,-108,29.4,-12.5,-108,29.4,-25.8,-108,29.4,-25.8,-108,11.9,-24.4,-129,-3.7,-12.5,-129,-3.7,-0.7,-129,-3.7,-0.7,-129,11.9,-0.7,-129,27.5,-12.5,-129,27.5,-24.4,-129,27.5,-24.4,-129,11.9,-23.1,-149,-2,-12.5,-149,-2,-2,-149,-2,-2,-149,11.9,-2,-149,25.8,-12.5,-149,25.8,-23.1,-149,25.8,-23.1,-149,11.9,-21.8,-166,-0.2,-12.5,-166,-0.2,-3.3,-166,-0.2,-3.3,-166,11.9,-3.3,-166,24.1,-12.5,-166,24.1,-21.8,-166,24.1,-21.8,-166,11.9,-20.3,-185,1.6,-12.5,-185,1.6,-4.8,-185,1.6,-4.8,-185,11.9,-4.8,-185,22.1,-12.5,-185,22.1,-20.3,-185,22.1,-20.3,-185,11.9,-19.1,-207,3.2,-12.5,-207,3.2,-6,-207,3.2,-6,-207,11.9,-6,-207,20.5,-12.5,-207,20.5,-19.1,-207,20.5,-19.1,-207,11.9,-17.8,-224,5,-12.5,-224,5,-7.3,-224,5,-7.3,-224,11.9,-7.3,-224,18.8,-12.5,-224,18.8,-17.8,-224,18.8,-17.8,-224,11.9,105.8,208,148.6,91.7,208,148.6,91.7,208,139.7,105.8,208,131,64.1,208,148.6,64.1,208,138.8,91.7,208,111.5,105.8,208,111.5,64.1,208,111.5,-87.5,207,148.6,-115.1,207,148.6,-115.1,207,139.7,-87.5,207,138.8,-129.1,207,148.6,-129.1,207,132.5,-115.1,207,111.5,-87.5,207,111.5,-129.1,207,111.5,105.4,208,-90.8,91.3,208,-90.8,91.3,208,-119,105.4,208,-110.3,63.6,208,-90.8,63.6,208,-118.1,82.9,208,-127.9,105.4,208,-127.9,63.6,208,-127.9,-87,208,-89.1,-114.7,208,-89.1,-114.7,208,-117.7,-87,208,-116.8,-128.8,208,-89.1,-128.8,208,-110.5,-106.2,208,-126.9,-87,208,-126.9,-128.8,208,-126.9,-45.5,115,89,-12.5,115,89,20.4,115,89,53.3,115,89,53.3,115,50.4,53.3,115,11.9,53.3,115,-26.6,53.3,115,-65.2,20.4,115,-65.2,-12.5,115,-65.2,-45.5,115,-65.2,-78.5,115,-65.2,-78.5,115,-26.6,-78.5,115,11.9,-78.5,115,50.4,-78.5,115,89,51,119,-61.2,51,119,-31.4,51,119,11.9,51,119,55.2,51,119,85,19.2,119,85,-12.5,119,85,-44.3,119,85,-76.1,119,85,-76.1,119,48.5,-76.1,119,11.9,-76.1,119,-24.6,-76.1,119,-61.2,-44.3,119,-61.2,-12.5,119,-61.2,19.2,119,-61.2,-67.3,170,-91.8,-87.9,170,-91.8,-103.7,170,-91.8,-103.7,170,-75.3,-103.7,170,-55.4,-90.7,170,-55.4,-67.3,170,-55.4,-67.3,170,-79.1,43.7,170,-92.4,43.7,170,-80,43.7,170,-57,66.6,170,-57,79.4,170,-57,79.4,170,-76,79.4,170,-92.4,63.8,170,-92.4,79.8,170,98.6,79.8,170,79.2,67.1,170,79.2,44.7,170,79.2,44.7,170,102.6,44.7,170,115.1,60.5,170,115.1,79.8,170,115.1,-85.2,170,115.5,-69.4,170,115.5,-69.4,170,103.1,-69.4,170,80.1,-91.8,170,80.1,-104.4,170,80.1,-104.4,170,99.3,-104.4,170,115.5,-2.4,-262,-1.5,-12.5,-262,-1.5,-12.5,-278,11.9,-2.4,-262,11.9,-22.7,-262,-1.5,-22.7,-262,11.9,-12.5,-262,25.3,-2.4,-262,25.3,-22.7,-262,25.3,-12.6,-320,10.6,-12.8,-340,10.6,-12.6,-279,11.7,-12.8,-299,11.7,23.3,35,11.9,23.3,35,53.8,-12.5,35,47.5,-48.4,35,53.8,-48.4,35,11.9,-48.4,35,-30,-12.5,35,-23.7,23.3,35,-30,25.3,45,56.2,-50.4,45,56.2,-50.4,45,-32.3,25.3,45,-32.3,27.7,58,59,-52.9,58,59,-52.9,58,-35.2,27.7,58,-35.2,29.8,69,61.5,-54.9,69,61.5,-54.9,69,-37.6,29.8,69,-37.6,29.9,130,-63.3,55.5,130,-63.3,55.5,130,-39.1,55.5,130,11.9,55.5,130,62.9,55.5,130,87.2,21.4,130,87.2,-12.5,130,87.2,-46.6,130,87.2,-80.7,130,87.2,-80.7,130,56.4,-80.7,130,11.9,-80.7,130,-32.6,-80.7,130,-63.3,-55.1,130,-63.3,-12.5,130,-63.3,66.4,150,-74.6,46.7,150,-74.6,6.9,150,-74.6,6.9,150,-44.6,6.9,150,-11.9,38.4,150,-11.9,66.4,150,-11.9,66.4,150,-54.3,-31.6,150,-74.4,-71.6,150,-74.4,-91.3,150,-74.4,-91.3,150,-50.8,-91.3,150,-11.5,-63.2,150,-11.5,-31.6,150,-11.5,-31.6,150,-44.4,24.6,159,-83.2,24.6,159,-61.6,24.6,159,-33.6,52,159,-33.6,72.7,159,-33.6,72.7,159,-62.5,72.7,159,-83.2,56.1,159,-83.2,-52.5,162,-84.6,-83.5,162,-84.6,-98.5,162,-84.6,-98.5,162,-65.7,-98.5,162,-37.2,-79.3,162,-37.2,-52.5,162,-37.2,-52.5,162,-64.8,49.6,181,-102.8,49.6,181,-91.2,49.6,181,-66.9,73.9,181,-66.9,87.1,181,-66.9,87.1,181,-85.8,87.1,181,-102.8,70,181,-102.8,56.8,195,-115.7,56.8,195,-105,56.8,195,-79.2,82.8,195,-79.2,96.5,195,-79.2,96.5,195,-100.5,96.5,195,-115.7,76.6,195,-115.7,-72.8,180,-101.6,-93.6,180,-101.6,-110.7,180,-101.6,-110.7,180,-86.1,-110.7,180,-64.9,-97.4,180,-64.9,-72.8,180,-64.9,-72.8,180,-89.7,-79.9,194,-114.2,-99.8,194,-114.2,-119.7,194,-114.2,-119.7,194,-99.7,-119.7,194,-77,-106,194,-77,-79.9,194,-77,-79.9,194,-103.2,90,185,91.9,76.8,185,91.9,52.3,185,91.9,52.3,185,116.8,52.3,185,128.3,74.6,185,128.3,90,185,128.3,90,185,111.4,96.9,195,100.5,83.4,195,100.5,57.5,195,100.5,57.5,195,126.5,57.5,195,137.2,83.4,195,137.2,96.9,195,137.2,96.9,195,122,-80.5,193,99.4,-106.1,193,99.4,-119.6,193,99.4,-119.6,193,121.5,-119.6,193,135.8,-106.1,193,135.8,-80.5,193,135.8,-80.5,193,125,-75.7,183,91.1,-100,183,91.1,-113.1,183,91.1,-113.1,183,112,-113.1,183,127.1,-97.7,183,127.1,-75.7,183,127.1,-75.7,183,115.6,12.5,104,-46.1,8.6,81,-40.3,2.3,45,-29.4,4.6,58,-33.4,6.5,69,-36.7,0.5,35,-26,-34.6,104,-46.1,-30.6,81,-40.3,-22.3,35,-25.4,-24.2,45,-29,-26.5,58,-33.2,-28.4,69,-36.6,-32.9,104,70,-29,81,64.1,-20.9,35,49,-22.7,45,52.6,-25,58,56.9,-26.9,69,60.4,9.4,104,70,5.8,81,64.1,-1.7,35,49.4,0,45,52.9,2,58,57.1,3.8,69,60.5,37.1,104,-17.5,-62.2,104,-17.5,32.1,81,-12.1,-57.2,81,-12.1,-48.4,35,-0.8,23.3,35,-0.8,-50.4,45,-3.4,25.3,45,-3.4,-52.9,58,-6.5,27.7,58,-6.5,-54.9,69,-9.2,29.8,69,-9.2,37.1,104,43.7,-62.2,104,43.7,32.1,81,38.3,-57.2,81,38.3,23.3,35,27.1,-48.4,35,27.1,25.3,45,29.7,-50.4,45,29.7,27.7,58,32.8,-52.9,58,32.8,29.8,69,35.4,-54.9,69,35.4,66.6,150,35.3,38.7,150,35.3,7.4,150,35.3,7.4,150,68.2,7.4,150,98.1,39.8,150,98.1,66.6,150,98.1,66.6,150,77.8,74.2,162,60.8,55.2,162,60.8,29,162,60.8,29,162,88.1,29,162,108,52.1,162,108,74.2,162,108,74.2,162,89.1,-99.9,163,64.4,-99.9,163,91.7,-99.9,163,109.4,-78.7,163,109.4,-56.3,163,109.4,-56.3,163,90.8,-56.3,163,64.4,-81.8,163,64.4,-92.2,151,37.9,-92.2,151,76.5,-92.2,151,99.1,-66.2,151,99.1,-34.2,151,99.1,-34.2,151,70.1,-34.2,151,37.9,-65.1,151,37.9,34.6,93,41,34.6,93,67.1,7.6,93,67.1,34.5,92,-14.8,34.5,92,-43.2,10.5,92,-43.2,-59.7,93,-14.8,-59.7,93,-43.2,-32.6,93,-43.2,-31.2,94,67.4,-60.1,94,67.4,-60.1,94,41.3]);
			for (var i:uint = 0; i < points.length/3; i++)
			{
				goalPos.push(new Num3D(points[3 * i ],points[3 * i+1], points[3 * i + 2]));
			}
			goalNum = goalPos.length;
		}
		public override function update(items:Array,num:uint):void 
		{
			_cur++;
			if (_cur < 10)
			{
				if (_pre) preMove(items, num, 1000);				
				return;
			}
			if (_cur<11)
			{
				for (var i:uint = 0; i < num; i++)
				{
					items[i].speed = new Num3D();
					items[i].opos = new Num3D();
					items[i].acceleration = new Num3D();
					items[i].damping = .95;					
				}
			}
			if (_cur < 20) return;
			if (_cur < 21+goalNum/_speed) 
			{
				addSpeed(items,(_cur-20)*_speed);
			}
			if (_cur >= 30  ) 
			{
				var kmax:uint = (_cur - 30) * _speed ;
				var addk:uint = _speed;
				if (kmax >goalNum)
				{
					addk =  goalNum + _speed-kmax;
					kmax = goalNum;
				}
				var tspeed:Num3D;
				var w:Number;
				var h:Number;
				if(kmax<goalNum)
					for (var k:uint = 0; k < addk; k++)
					{
						//items[(_cur - 30) * _speed + k].speed = new Vector3D();
						items[(_cur-30)*_speed+k].opos = items[(_cur-30)*_speed+k].pos.clone();
					}
				for (k=0; k < kmax; k++)
				{
					tspeed = items[k].opos.subtract(items[k].pos);
					items[k].damping = .7;
					tspeed.scaleBy(.1);
					items[k].acceleration = tspeed;
				}	/**/	
			}
		}
		
		public function addSpeed(items:Array,pcur:uint):void 
		{
			var left:uint = pcur + _speed;
			left = left > goalNum?goalNum:left;
			for (var i:uint = pcur; i < left;i++ )
			{
				var damping:Number = items[i].damping;
				damping = (1 - damping) / (1 - Math.pow(damping, 10));
				var tvc:Num3D = goalPos[i].subtract(items[i].pos);
				tvc.scaleBy(damping);
				items[i].speed = tvc;
			}
		}
		public function preMove(items:Array,num:uint,distance:Number):void 
		{
			var tvc:Num3D;
			var damping:Number;
			damping = .95;
			damping = (1 - damping) / (1 - Math.pow(damping, 10));
			for (var i:uint = goalNum; i < num; i++)
			{
				tvc = MathExtend.pInRound(i, 50, distance*.4+distance*.6*Math.random());
				tvc.decrementBy(items[i].pos);
				tvc.scaleBy(damping);
				items[i].speed = tvc;
			}
			this._pre = false;
		}
		
	}
	
}